package com.lanou3g.musicenjoy.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.android.volley.toolbox.ImageLoader;
import com.lanou3g.musicenjoy.R;
import com.lanou3g.musicenjoy.entity.NewSongList;
import com.lanou3g.musicenjoy.volley.VolleySingleton;
import com.squareup.picasso.Picasso;
import java.util.List;
import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by 李宁 on 15/10/20.
 * 新歌榜listView适配器
 */
public class NewSongListAdapter extends BaseAdapter {
    private NewSongList newSongList;//实体类
    private List<NewSongList.SongListEntity> song_list;//歌曲名 作者
    private Context mContext;
    ImageLoader imageLoader;//图片加载

    public NewSongListAdapter(Context mContext) {
        this.mContext = mContext;
        imageLoader = VolleySingleton.getInstance(mContext).getImageLoader();
    }

    //设置数据
    public void setData(List<NewSongList.SongListEntity> song_list) {
        this.song_list = song_list;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (song_list != null && song_list.size() > 0) {
            return song_list.size();
        } else
            return 0;
    }

    @Override
    public Object getItem(int position) {
        if (song_list != null && song_list.size() > 0) {
            return song_list.get(position);
        } else
            return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        //加载行布局文件
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_newsonglist, null);
            // 初始化缓存组件对象
            holder = new ViewHolder(convertView);

            convertView.setTag(holder);
        } else {
            //从convertView对象Tag属性中获取缓存组件对象
            holder = (ViewHolder) convertView.getTag();
        }
        NewSongList.SongListEntity entity = (NewSongList.SongListEntity) getItem(position);

        if (entity != null) {
            String url = entity.getPic_small();
            holder.newSongListFirst.setText(entity.getTitle());
            holder.newSongListFirstName.setText(entity.getAuthor());
            Picasso.with(mContext).load(url).into(holder.newSongListImageView);
        }

        return convertView;
    }

    /**
     * This class contains all butterknife-injected Views & Layouts from layout file 'item_newsonglist.xml'
     * for easy to all layout elements.
     *
     * @author ButterKnifeZelezny, plugin for Android Studio by Avast Developers (http://github.com/avast)
     */
    static
    //对象内部类,用于缓存行布局中需要改变内容的个别组件

    class ViewHolder {

        @Bind(R.id.newSongList_imageView)
        ImageView newSongListImageView;
        @Bind(R.id.newSongList_first)
        TextView newSongListFirst;
        @Bind(R.id.newSongList_first_name)
        TextView newSongListFirstName;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
